package gw.com.android.ui.views;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gwtsz.android.rxbus.RxBus;
import com.gwtsz.gts2.hx.R;

import gw.com.android.presenter.AdsConfigPresenter;
import gw.com.android.presenter.AdsData;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 走马灯公告
 * Created by ari on 2017/9/25.
 */
public class NoticeView extends RelativeLayout {

    private final String TAG = "NoticeView";

    private TextView textView;
    private static boolean show = true;
    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    public NoticeView(Context context) {
        this(context, null);
    }

    public NoticeView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NoticeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        View root = LayoutInflater.from(context).inflate(R.layout.layout_marquee, null);
        textView = root.findViewById(R.id.marquee_text);

        //textView的跑马灯设置
        textView.setSingleLine(true);
        textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
        textView.setMarqueeRepeatLimit(-1);
        textView.setSelected(true);

        ImageView ivRight = root.findViewById(R.id.marquee_right_icon);
        ivRight.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                marqueeClose();
                RxBus.getInstance().post(GTSConst.REPLY_MARQUEE_CLOSE, "");
            }
        });
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        addView(root, params);
        handle();

        Disposable disposable_notice = RxBus.getInstance().register(GTSConst.REPLY_RFRESH_ADS, String.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String jsonStr) {
                        handle();
                    }
                });
        mCompositeDisposable.add(disposable_notice);

        Disposable disposable_close = RxBus.getInstance().register(GTSConst.REPLY_MARQUEE_CLOSE, String.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(@NonNull String jsonStr) {
                        marqueeClose();
                    }
                });
        mCompositeDisposable.add(disposable_close);
    }

    private void marqueeClose() {
        show = false;//当关闭动作被执行，整个界面的
        setVisibility(GONE);
    }


    private void handle() {
        setVisibility(GONE);
        AdsData adData = AdsConfigPresenter.getInstance().getAd(AdsConfigPresenter.TYPE_NOTICE);
        if (adData == null || TextUtils.isEmpty(adData.content)) {
            return;
        }
        if (textView != null) {
            textView.setText(adData.content);
            super.setVisibility(show ? VISIBLE : GONE);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mCompositeDisposable != null) {
            mCompositeDisposable.clear();
        }
    }



}
